package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.PersGoSmartModeAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <Description> <br>
 *
 * @author zhouJiao<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2022年1月5日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler <br>
 */
@Service
@AttrHandler(PersGoSmartModeAttrHandler.class)
public class PersGoSmartModeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String attrValue = attr.getValue();
        String custId = data.getCustId();
        String prodInstId = inst.getInstId();
        String sceneInstId = data.getSceneInstId();

        //销售价处理
        KiteAttr sellAttr = querySceneInstService.getInstAttr(custId, InstType.PROD_INST, prodInstId, "200004107");//200004107为销售价属性编码
        //结算价处理
        KiteAttr settleAttr = querySceneInstService.getInstAttr(custId, InstType.PROD_INST, prodInstId, "200004108");

        // 人员通行智能管理应用平台销售价处理
        if (Objects.nonNull(sellAttr)) {
            //框架主产品销售价
            setHandleAttrValue ("110015079", attrValue, inst, "200004107", "3950", sceneInstId);//key:型号枚举值ID  newFieldName:销售价的属性编码   newValue:销售价的枚举值

            setHandleAttrValue ("110015080", attrValue, inst, "200004107", "3950", sceneInstId);

            setHandleAttrValue ("110015081", attrValue, inst, "200004107", "5150", sceneInstId);

            setHandleAttrValue ("110015082", attrValue, inst, "200004107", "5150", sceneInstId);
        }

        // 人员通行智能管理应用平台结算价处理
        if (Objects.nonNull(settleAttr)) {
            //框架主产品结算价
            setHandleAttrValue ("110015079", attrValue, inst, "200004108", "3400", sceneInstId);

            setHandleAttrValue ("110015080", attrValue, inst, "200004108", "3400", sceneInstId);

            setHandleAttrValue ("110015081", attrValue, inst, "200004108", "4450", sceneInstId);

            setHandleAttrValue ("110015082", attrValue, inst, "200004108", "4450", sceneInstId);
        }

        // 主框架型号与子框架的联动处理
        setOthProdInstMode(attrValue, inst, sceneInstId);

    }
    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }


    private void setHandleAttrValue (String key, String oldValue, KiteInst inst, String newFieldName,
                                     String newValue, String sceneInstId ) {
        if (KiteStringUtils.isEqual(key, oldValue)) {
            operateInstAttrService.setAttrValue(inst, newFieldName, newValue, sceneInstId, null);
        }
    }


    private void setOthProdInstMode(String value, KiteInst inst, String sceneInstId) {
        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(sceneInstId);
        kiteProdInstList.stream().forEach(prodInst ->{
            // 找到型号
            KiteAttr kiteAttr = prodInst.getAttr("200002772");

            if (Objects.nonNull(kiteAttr) && !KiteStringUtils.isEqual(inst.getInstId(), prodInst.getProdInstId())) {
                List<KiteAttrValue> kiteAttrValue = kiteAttr.getAttrValues();
                //所有含有款式A的属性联动
                if (KiteStringUtils.equalsAny(value, "110015079", "110015081")) {
                    List<KiteAttrValue> attrValues = kiteAttrValue.stream().filter(AttrValue -> KiteStringUtils.equalsAny(AttrValue.getAttrValue(), "110015079","110015081","110010415","110010417","110010419","110015083")).collect(Collectors.toList());
                    if (KiteListUtils.isNotEmpty(attrValues)) {
                        //型号联动(雁飞人员通行智能管理套件框架)
                        if("63249662".equals(prodInst.getSpecId())){   //"63249662"为雁飞人员通行智能管理套件框架的prodId
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), value, sceneInstId, null);
                        }
                        //型号联动(配件及其他)
                        if(("63249661".equals(prodInst.getSpecId())) && ("110015079".equals(value))){
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110010417", sceneInstId, null);
                        }
                        if(("63249661".equals(prodInst.getSpecId())) && ("110015081".equals(value))){
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110010419", sceneInstId, null);
                        }
                        //型号联动(电子元件)
                        if(("63249660".equals(prodInst.getSpecId()))){
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110010415", sceneInstId, null);
                        }
                        //型号联动(人员通行智能管理应用平台)
                        if(("63249663".equals(prodInst.getSpecId()))){
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110015083", sceneInstId, null);
                        }
                        //物资编码
                        if(KiteStringUtils.equalsAny(attrValues.get(0).getAttrValue(), "110010415", "110010417","110010419", "110015079","110015081")){
                            operateInstAttrService.setAttrValue(prodInst, "200004178", "7600301606", sceneInstId, null);
                        }
                        //产品公允价联动(电子元件)
                        if (("63249660".equals(prodInst.getSpecId())) &&(KiteStringUtils.isEqual("110015079", value)||KiteStringUtils.isEqual("110015081", value))) {
                            operateInstAttrService.setAttrValue(prodInst, "200004115", "2000", sceneInstId, null);
                        }
                        //产品公允价联动(配件及其它)
                        if(("63249661".equals(prodInst.getSpecId())) && KiteStringUtils.isEqual("110015079", value)){
                            operateInstAttrService.setAttrValue(prodInst, "200004115", "810", sceneInstId, null);
                        }
                        if(("63249661".equals(prodInst.getSpecId())) &&  KiteStringUtils.isEqual("110015081", value)){
                            operateInstAttrService.setAttrValue(prodInst, "200004115", "1860", sceneInstId, null);
                        }
                        //产品公允价联动(人员通行智能管理应用平台)
                        if (("63249663".equals(prodInst.getSpecId())) &&(KiteStringUtils.isEqual("110015079", value)||KiteStringUtils.isEqual("110015081", value))) {
                            operateInstAttrService.setAttrValue(prodInst, "200004115", "590", sceneInstId, null);
                        }
                        //销售价联动(延期属性组)
                        if((KiteStringUtils.isEqual("110015083", attrValues.get(0).getAttrValue())) || (KiteStringUtils.isEqual("110010419", attrValues.get(0).getAttrValue()))){
                            operateInstAttrService.setAttrValue(prodInst, "200004107", "666", sceneInstId, null);
                        }
                        //结算价联动(延期属性组)
                        if((KiteStringUtils.isEqual("110015083", attrValues.get(0).getAttrValue())) || (KiteStringUtils.isEqual("110010419", attrValues.get(0).getAttrValue()))){
                            operateInstAttrService.setAttrValue(prodInst, "200004108", "590", sceneInstId, null);
                        }
                    }
                }


                //所有含有款式D的属性联动
                if (KiteStringUtils.equalsAny(value, "110015080", "110015082")) {
                    List<KiteAttrValue> attrValues = kiteAttrValue.stream().filter(AttrValue -> KiteStringUtils.equalsAny(AttrValue.getAttrValue(),"110015080", "110015082", "110010416", "110015076","110015077","110015084")).collect(Collectors.toList());
                    if (KiteListUtils.isNotEmpty(attrValues)) {
                        //型号联动(雁飞人员通行智能管理套件框架)
                        if(("63249662".equals(prodInst.getSpecId()))){   //"63249662"为雁飞人员通行智能管理套件框架的prodId
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), value, sceneInstId, null);
                        }
                        //型号联动(配件及其他)
                        if(("63249661".equals(prodInst.getSpecId())) && ("110015080".equals(value))){
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110015076", sceneInstId, null);
                        }
                        if(("63249661".equals(prodInst.getSpecId())) && ("110015082".equals(value))){
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110015077", sceneInstId, null);
                        }
                        //型号联动(电子元件)
                        if(("63249660".equals(prodInst.getSpecId()))){
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110010416", sceneInstId, null);
                        }
                        //型号联动(人员通行智能管理应用平台)
                        if(("63249663".equals(prodInst.getSpecId()))){
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110015084", sceneInstId, null);
                        }
                        //物资编码联动
                        if(KiteStringUtils.equalsAny(attrValues.get(0).getAttrValue(), "110010416", "110015076","110015077", "110015080","110015082")){
                            operateInstAttrService.setAttrValue(prodInst, "200004178", "7600301075", sceneInstId, null);
                        }
                        //公允价联动(电子元件)
                        if (("63249660".equals(prodInst.getSpecId())) && (KiteStringUtils.isEqual("110015080", value)||KiteStringUtils.isEqual("110015082", value))) {
                            operateInstAttrService.setAttrValue(prodInst, "200004115", "2010", sceneInstId, null);
                        }
                        //公允价联动(配件及其他)
                        if(("63249661".equals(prodInst.getSpecId())) && KiteStringUtils.isEqual("110015080", value)){
                            operateInstAttrService.setAttrValue(prodInst, "200004115", "850", sceneInstId, null);
                        }
                        if(("63249661".equals(prodInst.getSpecId())) && KiteStringUtils.isEqual("110015082", value)){
                            operateInstAttrService.setAttrValue(prodInst, "200004115", "1900", sceneInstId, null);
                        }
                        //公允价联动(人员通行智能管理应用平台)
                        if (("63249663".equals(prodInst.getSpecId())) && (KiteStringUtils.isEqual("110015080", value)||KiteStringUtils.isEqual("110015082", value))) {
                            operateInstAttrService.setAttrValue(prodInst, "200004115", "540", sceneInstId, null);
                        }
                        //销售价联动(延期属性组)
                        if((KiteStringUtils.isEqual("110015084", attrValues.get(0).getAttrValue())) || (KiteStringUtils.isEqual("110010419", attrValues.get(0).getAttrValue()))){
                            operateInstAttrService.setAttrValue(prodInst, "200004107", "622", sceneInstId, null);
                        }
                        //结算价联动(延期属性组)
                        if((KiteStringUtils.isEqual("110015084", attrValues.get(0).getAttrValue())) || (KiteStringUtils.isEqual("110010419", attrValues.get(0).getAttrValue()))){
                            operateInstAttrService.setAttrValue(prodInst, "200004108", "540", sceneInstId, null);
                        }
                    }
                }
            }
        });
    }
}
